import requests
import json


class WeChatGroupRobot(object):
    """企业微信群机器人应用"""

    @staticmethod
    def send_data(warning_type, warning_msg):
        if warning_type == 'P3':
            send_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7be1c9a2-6032-4b0d-aa26-2ff2a699eaba'
        elif warning_type == 'P1P2':
            send_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=572a8ba9-df9b-4968-98d8-85580491ca25'
        else:
            raise ValueError

        data = {
            "msgtype": "markdown",
            "markdown": {
                "content": warning_msg,
            },
        }
        response = requests.post(send_url, data=json.dumps(data))
        response = response.json()  # 当返回的数据是json串的时候直接用.json即可将response转换成字典
        return response["errmsg"]


if __name__ == '__main__':
    wx = WeChatGroupRobot()
    msg = '<font color=\"warning\">测试【prod环境】 【p3】级别预警</font>\n' \
          '>容器编号：<font color=\"comment\">crawler-iscm-trace-air1-86646c88-js4jv</font>\n' \
          '>内核名称：<font color=\"comment\">crawler_trace_air_CI</font>\n' \
          '>问题描述：<font color=\"comment\">容器进程疑似Block，无法接受调度任务</font>\n' \
          '>发生时间：<font color=\"comment\">2022-05-20 15:27:59</font>'
    print(wx.send_data(warning_type='P1P2', warning_msg=msg))
